package cn.com.doone.common.uc.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.doone.common.uc.domain.rs.IRegisterJdbcRepository;
import cn.com.doone.common.uc.domain.rs.IRegisterLdapRepository;
import cn.com.doone.common.uc.service.IRegisterService;
import cn.com.doone.common.uc.service.dto.UserGroup;
import cn.com.doone.common.uc.service.dto.UserGroupAppAuthorize;
import cn.com.doone.common.uc.service.dto.UserGroupMember;
import cn.com.doone.common.uc.service.dto.UserInfo;
import cn.com.doone.common.uc.utils.MD5;
import cn.com.doone.common.uc.utils.PropertiesUtils;

@Service("registerService")
public class RegisterServiceImpl implements IRegisterService {
	
	@Autowired
	private IRegisterJdbcRepository registerJdbcRepository;
	
	@Autowired
	private IRegisterLdapRepository registerLdapRepository;
	
	@Autowired
	private PropertiesUtils propertiesUtils;

	@Override
	public boolean validUsername(String userAccount) {
    	return registerJdbcRepository.validUsername(userAccount);
	}

	@Override
	public boolean validPassword(String password) {
    	return registerJdbcRepository.validPassword(password);
	}

	@Override
	public void addUser(UserInfo userInfo, String tenantCode) throws Exception {
		int userInfoId = registerJdbcRepository.addUser(userInfo);
		userInfo.setUserInfoId(userInfoId);
		
		UserGroupMember userGroupMember = new UserGroupMember();
		userGroupMember.setUserInfoId(userInfoId);
		// 默认OA
		int userGroupId = registerJdbcRepository.findGroupIdByTenantCode(tenantCode);
		userGroupMember.setUserGroupId(userGroupId);
		
		registerJdbcRepository.addUserGroupMember(userGroupMember);
		
		// 若配置了LDAP服务器
		if (propertiesUtils.isLdapEnable()) {
			String tenantFullName = "OU=Account,dc=doone,dc=com,dc=cn";
			try {
				// 添加至LDAP服务器
				registerLdapRepository.addUser(tenantFullName, tenantCode, userInfo);
			} catch (Exception e) {
				registerLdapRepository.removeNode("CN=" + userInfo.getUserAccount() + "," + tenantFullName);
				throw e;
			}
		}
		
	}
	
	public void removeUser(String userAccount) throws Exception {
		registerJdbcRepository.removeUser(userAccount);
		
		// 若配置了LDAP服务器
		if (propertiesUtils.isLdapEnable()) {
			String tenantFullName = "OU=Account,dc=doone,dc=com,dc=cn";
			registerLdapRepository.removeUser(userAccount, tenantFullName);
		}
	}

	@Override
	public boolean validTenantCode(String tenantCode) {
		return registerJdbcRepository.validTenantCode(tenantCode);
	}

	@Override
	public void addTenant(UserGroup userGroup) {
		registerJdbcRepository.addTenant(userGroup);
	}
	
	@Override
	public void updateTenant(UserGroup userGroup) throws Exception {
		registerJdbcRepository.updateTenant(userGroup);
		
		// 若配置了LDAP服务器
		if (propertiesUtils.isLdapEnable()) {
			registerLdapRepository.updateTenant(userGroup);
		}
	}

	@Override
	public String registerUser(UserInfo userInfo, String tenantName) throws Exception {
		
		// 添加用户
		int userInfoId = registerJdbcRepository.addUser(userInfo);
		
		// 添加租户
		UserGroup userGroup = new UserGroup();
		
		userGroup.setGroupName(tenantName);
		userGroup.setGroupType("1");
		userGroup.setTenantCode(MD5.getMD5Str(userInfo.getUserAccount()));
		
		int userGroupId = registerJdbcRepository.addTenant(userGroup);
		
		// 若配置了LDAP服务器
		if (propertiesUtils.isLdapEnable()) {
			String tenantFullName = "OU=Account,dc=doone,dc=com,dc=cn";
			try {
				// 添加至LDAP服务器
				registerLdapRepository.addUser(tenantFullName, userGroup.getTenantCode(), userInfo);
			} catch (Exception e) {
				registerLdapRepository.removeNode("CN=" + userInfo.getUserAccount() + "," + tenantFullName);
				throw e;
			}		
		}
		
		// 用户添加至组
		UserGroupMember userGroupMember = new UserGroupMember();
		userGroupMember.setUserInfoId(userInfoId);
		userGroupMember.setUserGroupId(userGroupId);
		
		registerJdbcRepository.addUserGroupMember(userGroupMember);
		
		// 添加租户授权
		UserGroupAppAuthorize userGroupAppAuthorize = new UserGroupAppAuthorize();
		
		int appInfoId = registerJdbcRepository.findAppInfoIdByAppCode("XDW_OA_P");
		
		userGroupAppAuthorize.setAppInfoId(appInfoId);
		userGroupAppAuthorize.setUserGroupId(userGroupId);
		
		registerJdbcRepository.addGroupAppAuthorize(userGroupAppAuthorize);
		
		return userGroup.getTenantCode();
		
	}

	@Override
	public void addUserGroupMember(UserGroupMember userGroupMember) {
		registerJdbcRepository.addUserGroupMember(userGroupMember);
	}

	@Override
	public int findGroupIdByTenantCode(String tenantCode) {
		return registerJdbcRepository.findGroupIdByTenantCode(tenantCode);
	}

	@Override
	public void inviteUser(UserInfo userInfo, String tenantCode) throws Exception {
		int userInfoId = registerJdbcRepository.addUser(userInfo);
		int userGroupId = registerJdbcRepository.findGroupIdByTenantCode(tenantCode);
		
		UserGroupMember userGroupMember = new UserGroupMember();
		userGroupMember.setUserInfoId(userInfoId);
		userGroupMember.setUserGroupId(userGroupId);
		
		registerJdbcRepository.addUserGroupMember(userGroupMember);
		
		// 若配置了LDAP服务器
		if (propertiesUtils.isLdapEnable()) {
			String tenantFullName = "";
			try {
				tenantFullName = registerLdapRepository.findTenantFullNameByTenantCode(tenantCode);
				registerLdapRepository.addUser(tenantFullName, tenantCode, userInfo);
			} catch (Exception e) {
				registerLdapRepository.removeNode("CN=" + userInfo.getUserAccount() + "," + tenantFullName);
				throw e;
			}		
		}
		
	}

	@Override
	public void syncUser() throws Exception {
		
		System.out.println("同步开始~~~~~~~~");
		
		// 若配置了LDAP服务器
		if (propertiesUtils.isLdapEnable()) {
			List<Map<String, Object>> userMapList = registerJdbcRepository.findNotSyncUser();
			registerLdapRepository.sysnUser(userMapList);
			registerJdbcRepository.updateSync();
		}
		
	}

	@Override
	public String resetPassword(String userAccount) {
		String pwd = registerJdbcRepository.resetPassword(userAccount);
		
		// 若配置了LDAP服务器
		if (propertiesUtils.isLdapEnable()) {
			String tenantFullName = "OU=Account,dc=doone,dc=com,dc=cn";
			registerLdapRepository.resetPassword(userAccount, tenantFullName,pwd);
		}

		return pwd;
	}

}
